<template>
  <flexbox class="xr-header">
    <div v-if="!!iconClass" :style="{ backgroundColor: iconColor }" class="xr-header__icon">
      <i :class="iconClass"/>
    </div>

    <div class="xr-header__label">
      <slot v-if="$slots.label" name="label"/>
      <template v-else>{{ label }}</template>
    </div>

    <el-input v-if="showSearch" :placeholder="placeholder"
              v-model="search" v-bind="inputAttr"
              :class="{'is-text': searchIconType === 'text'}"
              :style="{'margin-top': ftTop}"
              class="xr-header__search"
              @input="inputChange"
              @keyup.enter="searchClick">

      <template v-slot:append>
        <el-button v-if="searchIconType === 'text'" type="primary" @click="searchClick">
          搜索
        </el-button>
        <el-button v-else icon="el-icon-search" @click="searchClick"/>
      </template>
    </el-input>

    <div :style="{ top: ftTop }" class="xr-header__ft">
      <!--todo whf ft 是不是foot的意思-->
      <slot name="ft"/>
    </div>
  </flexbox>
</template>

<script setup>

defineOptions({
  // 公共列表搜索头部
  name: 'XrHeader',
})

const props = defineProps({
  iconClass: [String, Array],
  iconColor: String,
  label: String,
  // value: String,

  showSearch: {
    type: Boolean,
    default: true
  },
  // 图标形式的搜索
  searchIconType: {
    type: String,
    default: 'text' // icon
  },
  placeholder: {
    type: String,
    default: '请输入内容'
  },
  ftTop: {
    type: String,
    default: '15px'
  },

  content: [String, Number],

  inputAttr: {
    type: Object,
    default: () => {
    }
  }
})

const emits = defineEmits(['update:content', 'search'])

const optionData = reactive({
  search: ''
})
const {search} = toRefs(optionData)

watch(() => props.content, (val) => {
  if (optionData.search != props.content) {
    optionData.search = props.content
  }
}, {
  immediate: true
})


function inputChange() {
  emits('update:content', optionData.search)
}

function searchClick() {
  emits('search', optionData.search)
}
</script>

<style lang="scss" scoped>
.xr-header {
  padding: 15px;
  position: relative;

  &__icon {
    width: 30px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    border-radius: $xr-border-radius-base;
    margin-right: 10px;

    .wk {
      color: white;
      font-size: 18px;
    }
  }

  &__label {
    font-size: 16px;
    color: #333333;
    font-weight: 600;
  }

  &__search {
    width: 300px;
    margin: 0 0 0 -150px;
    position: absolute;
    left: 50%;
    top: 0;
  }

  &__search.is-text {
    :deep(.el-input-group__append) {
      background-color: $xr-color-primary;
      border-color: $xr-color-primary;
      color: white;
    }
  }

  &__ft {
    position: absolute;
    right: 15px;
  }
}
</style>
